package com.sakhalin.news.network

import com.google.gson.GsonBuilder
import com.sakhalin.news.models.news.NewsCategoryItem
import com.sakhalin.news.network.responses.BaseResponse
import com.sakhalin.news.network.responses.NewsDetailsResponse
import com.sakhalin.news.network.responses.NewsListResponse
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query

interface NewsApi {

    companion object {

        var guid: String = "guid"
        var build: Int = 1
        var version: String = "1.0"
        var token: String = "guest"
        const val USER_AGENT: String = "AndroidApp"

        private var apiService: NewsApi? = null

        fun getInstance(): NewsApi {

            val httpLoggingInterceptor = HttpLoggingInterceptor()
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)

            if (apiService == null) {

                val httpClient = OkHttpClient.Builder()
                    .addNetworkInterceptor { chain ->
                        chain.proceed(
                            chain.request()
                                .newBuilder()
                                .removeHeader("User-Agent")
                                .addHeader("User-Agent", USER_AGENT)
                                .addHeader("Cookie", getCookieString())
                                .build()
                        )
                    }
                    .addInterceptor(httpLoggingInterceptor)
                    .build()

                apiService = Retrofit.Builder()
                    .baseUrl(ApiConstants.API_BASE_URL)
                    .addConverterFactory(
                        GsonConverterFactory.create(
                            createGsonBuilder().create()
                        )
                    )
                    .client(httpClient)
                    .build().create(NewsApi::class.java)
            }

            return apiService!!
        }

        private fun createGsonBuilder(): GsonBuilder {
            val gsonBuilder = GsonBuilder()
            return gsonBuilder.create().newBuilder()
        }

        private fun getCookieString(): String {
            return "guid=${guid};token=${token};version=${build}"
        }
    }

    @GET(ApiConstants.NEWS_CATEGORIES_ENDPOINT)
    suspend fun getNewsCategories(): BaseResponse<List<NewsCategoryItem>>

    @GET(ApiConstants.NEWS_LIST_BY_CATEGORY_ENDPOINT)
    suspend fun getNewsListByCategory(@Query("rubric") id: String): NewsListResponse

    @GET(ApiConstants.NEWS_LIST_BY_CATEGORY_AND_DATE_ENDPOINT)
    suspend fun getNewsListByCategory(
        @Query("rubric") id: String,
        @Query("date") date: String
    ): NewsListResponse

    @GET(ApiConstants.NEWS_DETAILS_BY_ID_ENDPOINT)
    suspend fun getNewsDetails(@Query("id") id: String, @Query("dark_mode") dark_mode: Boolean = false): NewsDetailsResponse
}